package hufs.twotwoco.manager;

import hufs.twotwoco.object.ManageObject;
import hufs.twotwoco.object.ServerBean;
import hufs.twotwoco.object.SettingBean;
import hufs.twotwoco.object.UserObject;
import hufs.twotwoco.pmf.PMF;
import hufs.twotwoco.var.Var;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

public class ServerManagerServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {
			ServerBean serverInfo = new ServerBean(user.getNickname(),
					user.getEmail());
			req.setAttribute("server_info", serverInfo);
		}
		SettingBean settings = new SettingBean();
		req.setAttribute("settings", settings);
		/*
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		 * String query = "SELECT FROM" + ManageObject.class.getName(); try {
		 * List<ManageObject> s = (List<ManageObject>) pm.newQuery(query)
		 * .execute(); if (s.size() > 0) { for (int i = 0; i < s.size(); i++) {
		 * ManageObject order = s.get(i); JSONObject shjo = new JSONObject();
		 * shjo.put(Var.KEY_KEY, order.getKey()); shjo.put(Var.KEY_UID,
		 * order.getUid()); shjo.put(Var.KEY_NAME, order.getName());
		 * shjo.put(Var.KEY_PRICE, order.getPrice()); shjo.put(Var.KEY_NUM,
		 * order.getNum()); shjo.put(Var.KEY_MILK, order.getMilk());
		 * shjo.put(Var.KEY_SHOT, order.getShot()); shjo.put(Var.KEY_SIZE,
		 * order.getSize()); shjo.put(Var.KEY_WHIPPING, order.getWhipping());
		 * shjo.put(Var.KEY_RECEIVE, order.getReceive()); shop.put(shjo); }
		 * req.setAttribute("shops", shop); } } catch (JSONException e) { //
		 * TODO Auto-generated catch block e.printStackTrace(); } finally {
		 * pm.close(); }
		 
		String query = "SELECT FROM" + UserObject.class.getName();
		try {
			List<UserObject> s = (List<UserObject>) pm.newQuery(query)
					.execute();
			if (s.size() > 0) {
				req.setAttribute("shops", s);
			}*/
			String url = "/templates/index.jsp";
			try {
				req.getRequestDispatcher(url).forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		/*} finally {
			pm.close();
		}*/
		/*
		 * String url = "/templates/index.jsp"; try {
		 * req.getRequestDispatcher(url).forward(req, resp); } catch
		 * (ServletException e) { e.printStackTrace(); }
		 */
		return;
	}
}
